package com.crawler.waf.support;

import java.util.List;

import org.springframework.security.core.context.SecurityContextHolder;

import com.crawler.waf.exceptions.handlers.ErrorMessageQueueHandler;
import com.crawler.waf.security.authens.BaseUserInfo;
import com.crawler.waf.security.authens.UserRole;
import com.crawler.waf.security.authens.UserToken;
import com.crawler.waf.security.authens.UserCenterUserDetails;

/**
 * 提供给系统的全局访问认证用户的信息。
 *
 */
public final class WafContext {			
	/**
	 * 认证用户的相关详细信息，其中包括用户信息，角色信息，用户领域属性，用户区域属性以及主体和token信息
	 */
	public static UserCenterUserDetails getCurrentDetails() {

		UserCenterUserDetails details = (UserCenterUserDetails) SecurityContextHolder.getContext().getAuthentication().getDetails();
		return details;
	}

	/**
	 * 获得当前登录用户的信息
	 * 
	 * @return
	 */
	public static BaseUserInfo getCurrertUserInfo() {
		Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		if(principal instanceof BaseUserInfo){			
			BaseUserInfo userInfo = (BaseUserInfo) principal;
			return userInfo;
		}
		else{
			return null;
		}
		
	}

	/**
	 * 获得当前登录用户的验证凭据，token
	 * 
	 * @return
	 */
	public static UserToken getCurrentToken() {
		UserToken token = (UserToken) SecurityContextHolder.getContext().getAuthentication().getCredentials();
		return token;
	}

	/**
	 * 获得当前用户的登录角色信息
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static List<UserRole> getCurrentUserRoles() {
		List<UserRole> roles = (List<UserRole>) SecurityContextHolder.getContext().getAuthentication().getAuthorities();
		return roles;
	}
	
	/**
	 * 
	* @Title: setCapacity 
	* @Description: 设置异常队列的容量大小
	* @param @param capacity    设定文件 
	* @return void    返回类型 
	* @throws
	 */
	public static void setCapacity(int capacity){
		ErrorMessageQueueHandler.setCapacity(capacity);
	}
}
